﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Player
{
    public class DownloadThread : WorkerThread<EventArgs>
    {
        private FrameBuffer m_frames;
        private SessionBase m_session;

        public DownloadThread(SessionBase session, FrameBuffer frames)
        {
            m_session = session;
            m_frames = frames;
        }

        protected override void Run(params object[] args)
        {
            long startTime = (long)args[0];

            foreach (Frame frame in m_session.LoadFrames(startTime))
            {
                if (!BlockAndWaitForStopOrResult(
                                                (o) => m_frames.Write((Frame)o),
                                                frame,
                                           TimeSpan.FromMilliseconds(-1)))
                {
                    break;
                }

                Thread.Sleep(0);
            }
        }

        public override void Stop()
        {
            m_frames.StopThread();
            base.Stop();
        }
    }
}
